package com.hegaoye.mqhandler;

import com.alibaba.fastjson.JSON;
import com.hegaoye.base.KafkaConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/** Created by lixin on 2019/6/29. */
@Slf4j
@Component
public class OrderReceiver {
    //注释的为业务接口调用情况
  //    @Resource
  //    private GoodsSV goodsSV;

  //    @Resource
  //    private DiscountSV discountSV;

  @KafkaListener(topics = {KafkaConfig.ORDER_DISCOUNT_QUEUE_NAME})
  public void process(String msg) {
    log.info("OrderReceiver >> " + msg);
    Map<String, String> param = JSON.parseObject(msg, Map.class);
    this.reduceStock(param.get("goodsCode"));
    this.addDiscount(param.get("userCode"));
  }

  private void reduceStock(String goodsCode) {
    Map<String, Object> param = new HashMap<>();
    param.put("code", goodsCode);
    log.info("reduceStock:" + goodsCode);
    //        Goods goods = goodsSV.load(param);
    //        if (goods == null) {
    //            throw new GoodsException(BaseException.BaseExceptionEnum.Result_Not_Exist);
    //        }
    //        goods.setStock(goods.getStock() - 1);
    //        goodsSV.saveOrUpdate(goods);
  }

  private void addDiscount(String userCode) {
    log.info("reduceStock:" + userCode);
    //        Discount discount = new Discount();
    //        discount.setUserCode(userCode);
    //        discount.setDiscountLevel(8L);
    //        discountSV.save(discount);
  }
}
